package de.htwg_konstanz.ebus.framework.webservices;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.w3c.dom.Document;

import de.htwg_konstanz.ebus.framework.impEx.IExporter;
import de.htwg_konstanz.ebus.framework.impEx.XmlExporter;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.BMECAT;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServiceFault;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServicePortType;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServiceRequest;
import de.htwg_konstanz.ebus.framework.webservices.productcatalog.ProductCatalogServiceResponse;

@WebService(name="ProductCatalogWebService") 
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public class ProductCatalogWebService implements ProductCatalogServicePortType {
	
	@WebMethod
	@WebResult(name = "ProductCatalogServiceResponse", targetNamespace = "http://www.htwg-konstanz.de/ebsf/ws/product_catalog")
	public ProductCatalogServiceResponse getProductCatalog(
			@WebParam(name = "ProductCatalogServiceRequest", targetNamespace = "http://www.htwg-konstanz.de/ebsf/ws/product_catalog") ProductCatalogServiceRequest request) 
				throws ProductCatalogServiceFault {
			
		//TODO: validate user data
		
		JAXBElement<BMECAT> root = null;
		
		JAXBContext jc;
		try {
			IExporter exporter = new XmlExporter();
			exporter.performExport();
			Document doc = exporter.getXmlDocument();
			
			System.out.println(exporter.getXmlString());
			
			jc = JAXBContext.newInstance(BMECAT.class);
			Unmarshaller um = jc.createUnmarshaller();
			um.setSchema(null);
			root = um.unmarshal(doc, BMECAT.class);
			
			System.out.println("----> " + root.getValue().getHEADER());
			
			
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ProductCatalogServiceResponse catalogServiceResponse = new ProductCatalogServiceResponse();
		catalogServiceResponse.setBMECAT(root.getValue());
		
		return catalogServiceResponse;
	}
	
}
